package com.jy.adscm.operation.lxcg.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.lxcg.bean.*;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgMaterialMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgSupplierMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgTaskDetailMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgTaskMapper;
import com.jy.adscm.operation.lxcg.service.LxcgTaskService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 零星采购任务 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@Service
public class LxcgTaskServiceImpl extends ServiceImpl<LxcgTaskMapper, LxcgTask> implements LxcgTaskService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    LxcgMaterialMapper lxcgMaterialMapper;
    @Autowired
    LxcgTaskDetailMapper lxcgTaskDetailMapper;
    @Autowired
    LxcgSupplierMapper lxcgSupplierMapper;
    @Autowired
    JyDeptMapper jyDeptMapper;
    @Override
    public boolean updateLxcgTask(LxcgTask lxcgTask, Token token) throws AdscmSystemException {
        LxcgTask oldLxcgTask = super.selectById(lxcgTask.getId());
        //判断一下如果老三个都不等
        if (!(oldLxcgTask.getIsArrive()==1 && oldLxcgTask.getIsDeliver()==1
                &&oldLxcgTask.getIsPay()==1&&oldLxcgTask.getIsReceiveInvoice()==1)){
            if (lxcgTask.getIsArrive()==1 && lxcgTask.getIsDeliver()==1
                    &&lxcgTask.getIsPay()==1&&lxcgTask.getIsReceiveInvoice()==1){
                lxcgTask.setState(1);
            }
        }

        //启动任务
        if (lxcgTask.getIsStart()==1&&lxcgTask.getState()==0){
            lxcgTask.setStartTime(new Date());
            lxcgTask.setCgPerson(token.getUser().getId().toString());
        }
        //结束任务
        if (lxcgTask.getIsReceiveInvoice()==1&&lxcgTask.getState()==1){
            lxcgTask.setTaskFinishTime(new Date());
        }
        //开始备货
        if (lxcgTask.getIsStockUp()==1){
            if (oldLxcgTask.getIsStockUp()==0){
                lxcgTask.setStockUpTime(new Date());
            }
        }
        //备货检测
        if (lxcgTask.getIsCheckUp()==1){
            if (oldLxcgTask.getIsCheckUp()==0){
                lxcgTask.setCheckUpTime(new Date());
            }
        }
        //发货
        if (lxcgTask.getIsDeliver()==1){
            if (oldLxcgTask.getIsDeliver()==0){
                lxcgTask.setDeliverTime(new Date());
            }
        }
        //到货
        if (lxcgTask.getIsArrive()==1){
            if (oldLxcgTask.getIsArrive()==0){
                lxcgTask.setDeliverTime(new Date());
                lxcgTask.setKfPerson(token.getUser().getId().toString());
            }
        }
        //付款
        if (lxcgTask.getIsPay()==1){
            if (oldLxcgTask.getIsPay()==0){
                lxcgTask.setPayAddTime(new Date());
                lxcgTask.setCwPerson(token.getUser().getId().toString());
            }
        }
        //收票
        if (lxcgTask.getIsReceiveInvoice()==1){
            if (oldLxcgTask.getIsReceiveInvoice()==0){
                lxcgTask.setReceiveInvoiceAddTime(new Date());
                lxcgTask.setCwPerson(token.getUser().getId().toString());
            }
        }
        boolean b = super.updateById(lxcgTask);
        return b;
    }

    @Override
    public Page<LxcgTask> selectLxcgTask(Page<LxcgTask> page, QueryLxcgTask queryLxcgTask,Token token) throws AdscmSystemException {
        Wrapper<LxcgTask> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getMakeNo(),null)){
            wrapper.like("make_no",queryLxcgTask.getMakeNo());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getOrderNums(),null)){
            wrapper.like("order_nums",queryLxcgTask.getOrderNums());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getSupplier(),null)){
            wrapper.eq("supplier",queryLxcgTask.getSupplier());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getState(),null)){
            wrapper.eq("state",queryLxcgTask.getState());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getIsArrive(),null)){
            wrapper.eq("is_arrive",queryLxcgTask.getIsArrive());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgTask.getIsPay(),null)){
            wrapper.eq("is_pay",queryLxcgTask.getIsPay());
        }
        wrapper.orderBy("create_time",false);
        page = super.selectPage(page,wrapper);
        List<LxcgTask> lxcgTasks = page.getRecords();
        for (LxcgTask lxcgTask : lxcgTasks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgTask.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgTask.getCreator());
                lxcgTask.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgTask.getCgPerson(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgTask.getCgPerson());
                lxcgTask.setCgPersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgTask.getKfPerson(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgTask.getKfPerson());
                lxcgTask.setKfPersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgTask.getCwPerson(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgTask.getCwPerson());
                lxcgTask.setCwPersonName(jyUser.getName());
            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTask.getCreatorDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(lxcgTask.getCreatorDept());
                lxcgTask.setCreatorDeptName(jyDept.getDeptName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTask.getSupplier(),null)){
                LxcgSupplier lxcgSupplier = lxcgSupplierMapper.selectById(lxcgTask.getSupplier());
                lxcgTask.setSupplierName(lxcgSupplier);
            }
            //查询订单物料
            Float tempTotals=0f;
            Wrapper<LxcgTaskDetail> lxcgTaskDetailWrapper=new EntityWrapper<>();
            lxcgTaskDetailWrapper.eq("tid",lxcgTask.getId());
            List<LxcgTaskDetail> lxcgTaskDetails = lxcgTaskDetailMapper.selectList(lxcgTaskDetailWrapper);
            String  lxcgDetail="";
            for (LxcgTaskDetail lxcgTaskDetail : lxcgTaskDetails) {
                if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgTaskDetail.getActTotal(),null)){
                    tempTotals=tempTotals+ lxcgTaskDetail.getActTotal();
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getBomName(),null)){
                    lxcgDetail=lxcgDetail+" "+lxcgTaskDetail.getBomName();
                }
            }
            lxcgTask.setTotals(tempTotals);
            lxcgTask.setLxcgTaskDetail(lxcgDetail);
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public boolean insertLxcgTask(LxcgTask lxcgTask, Token token) {

        //查询最大单号
        String  makeNo=   new SimpleDateFormat("yyMMdd").format(new Date());
        Wrapper<LxcgTask> wrapper=new EntityWrapper<>();
        wrapper.like("make_no",makeNo, SqlLike.RIGHT);
        wrapper.orderBy("make_no",false);
        wrapper.last("limit 1");
        List<LxcgTask> lxcgTasks = super.selectList(wrapper);
        if (lxcgTasks!=null && lxcgTasks.size()>0){
            LxcgTask lxcgTask1 = lxcgTasks.get(0);
          makeNo=String.valueOf(Integer.valueOf(lxcgTask1.getMakeNo())+1)  ;
        }else {
            makeNo=makeNo+"01";
        }
        lxcgTask.setCreator(token.getUser().getId().toString());
        lxcgTask.setCreateTime(new Date());
        lxcgTask.setIsArrive(0);
        lxcgTask.setIsCheckUp(0);
        lxcgTask.setIsDeliver(0);
        lxcgTask.setIsPay(0);
        lxcgTask.setIsReceiveInvoice(0);
        lxcgTask.setIsStart(0);
        lxcgTask.setIsStockUp(0);
        lxcgTask.setState(0);
        lxcgTask.setMakeNo(makeNo);
        lxcgTask.setCreatorDept(token.getUser().getDeptId().toString());
        boolean insert = super.insert(lxcgTask);
        return insert;
    }
}
